﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Print_Print2d : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["DenNgayPhuLuc2d"] != null && Session["DenNgayPhuLuc2d"] != null)
            {
                try
                {
                    binnding();
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment; filename=Phuluc2d.xls");
                    Response.Charset = "";
                    Response.ContentType = "application/vnd.xls";
                    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                    grid_Detail.RenderControl(htmlWrite);
                    Response.Write(stringWrite.ToString());
                    Response.End();
                    Response.Redirect("javascript:window.close()");
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('" + ex.Message + "')</script>");
                }
                Session.Remove("TuNgayPhuLuc2d");
                Session.Remove("DenNgayPhuLuc2d");
            }
        }
    }
    //hàm thống kê
    public void thongke(List<tblCoSoDoan> list_csd,List<tblGiaiTheDonViDoan> listgiaithedv, int sothutu, string tieude, string daidien, Label label_alias)
    {

        int thanhlapmoi = Get_count_by_creat_delete(list_csd, listgiaithedv, 1, daidien);
        int giaithe = Get_count_by_creat_delete(list_csd, listgiaithedv, 0, daidien);
        int khoitruonghoc = Get_list_by_doankhoi(list_csd, daidien, 85);//85:mã danh muc :khối trường học
        int khoidoanhnghiep_nhanuoc = Get_list_by_doankhoi(list_csd, daidien, 86);//86:mã danh mục khối dn nhà nước
        int khoidoanhnghiep_ngoai_nhanuoc = Get_list_by_doankhoi(list_csd, daidien, 87);//87:mã danh mục khối dn ngoài nhà nước
        int khoiCCVC = Get_list_by_doankhoi(list_csd, daidien, 88);//86:mã danh mục khối công chức viên chức
        int khoiLLVT = Get_list_by_doankhoi(list_csd, daidien, 89);//86:mã danh mục khối lực lượng vũ trang
        int khoinongthon = Get_list_by_doankhoi(list_csd, daidien, 105);//86:mã danh mục khối nong thon
        int khoidothi = Get_list_by_doankhoi(list_csd, daidien, 106);//86:mã danh mục khối do thi
        int khoikhac = Get_list_by_doankhoi(list_csd, daidien, 107);//107:khối khác
        int tong = thanhlapmoi + giaithe + khoitruonghoc + khoidoanhnghiep_ngoai_nhanuoc + khoidoanhnghiep_nhanuoc + khoiCCVC + khoiLLVT + khoinongthon + khoidothi + khoikhac;
        string str = "<td >" + sothutu + "</td>" +
                    "<td  style='text-align: center'>" + tieude + "</td>" +
                    "<td  style='text-align: center'>" + tong + "</td>" +
                    "<td  style='text-align: center'>" + thanhlapmoi + "</td>" +
                    "<td  style='text-align: center'>" + giaithe + "</td>" +
                    "<td  style='text-align: center'>" + khoinongthon + "</td>" +
                    "<td  style='text-align: center'>" + khoidothi + "</td>" +
                    "<td  style='text-align: center'>" + khoitruonghoc + "</td>" +
                    "<td  style='text-align: center'>" + khoidoanhnghiep_nhanuoc + "</td>" +
                    "<td  style='text-align: center'>" + khoidoanhnghiep_ngoai_nhanuoc + "</td>" +
                    "<td  style='text-align: center'>" + khoiCCVC + "</td>" +
                    "<td  style='text-align: center'>" + khoiLLVT + "</td>" +
                    "<td  style='text-align: center'>" + khoikhac + "</td>";
        label_alias.Text = str;

    }
    //co so doan giai the,thanh lap moi
    public int Get_count_by_creat_delete(List<tblCoSoDoan> listcosodoan,List<tblGiaiTheDonViDoan> listgiaithedv, int trangthai, string daidien)
    {
        int soluong = 0;
        if (trangthai == 1)//1 thanh lap moi
            {
                soluong = listcosodoan.Where(r => r.MaDonVi.Trim().Substring(0, 3) == daidien.Trim()).Count();
            }   
        else if (trangthai == 0) // 0 giai the
            {
                //tinh so luong co so doan bi giai the, chua lam
                soluong = listgiaithedv.Where(r => r.MaDonVi.Trim().Substring(0, 3) == daidien.Trim()).Count();
            }
        return soluong;
    }
    //thieu khoi dia ban dan cu,nong thon hoac do thi cho coso doan
    public int Get_count_by_area(List<tblCoSoDoan> listcosodoan, string daidien)
    {
        return listcosodoan.Where(r => r.MaDonVi.Trim().Substring(0, 3) == daidien.Trim()).Count();
    }
    /// <summary>
    /// Dem so luong co so doan theo doan khoi
    /// </summary>
    /// <param name="list_cosodoanvien">list co so doan</param>
    /// <param name="daidien">ma dai dien co so doan</param>
    /// <param name="madoankhoi">ma doan khoi</param>
    /// <returns></returns>
    public int Get_list_by_doankhoi(List<tblCoSoDoan> list_cosodoanvien, string daidien, int madoankhoi)
    {
        return list_cosodoanvien.Where(r => r.MaDonVi.Substring(0, 3).Trim() == daidien && r.DoanKhoiID == madoankhoi).Count();
    }
    public void binnding()
    {
        _dbDataContext _db = new _dbDataContext();
        DateTime tungay_dt = CatalogAccess.ConvertToDate(Session["TuNgayPhuLuc2d"].ToString());
        DateTime toingay_dt = CatalogAccess.ConvertToDate(Session["DenNgayPhuLuc2d"].ToString());
        var listcosodoan = _db.tblCoSoDoans.Where(r => r.NgayThanhLap >= tungay_dt && r.NgayThanhLap <= toingay_dt).ToList();
        var listgiaithecsd = _db.tblGiaiTheDonViDoans.Where(r => r.NgayGiaiThe >= tungay_dt && r.NgayGiaiThe <= toingay_dt).ToList();
        thongke(listcosodoan, listgiaithecsd, 1, "Quận, huyện, thị, thành đoàn", "CSD", lbl_quanhuyendoan);
        thongke(listcosodoan,listgiaithecsd, 2, "Đoàn tương đương cấp huyện", "CSD", lbl_doantuongduonghuyen);
        thongke(listcosodoan,listgiaithecsd, 3, "Đoàn cơ sở", "DCS", lbl_doancoso);
        thongke(listcosodoan,listgiaithecsd, 4, "Chi đoàn cơ sở", "CDC", lbl_chidoancoso);
        thongke(listcosodoan,listgiaithecsd, 5, "Chi đoàn", "CDD", lbl_chidoan);
    }
}